package com.medica.ledproject.service.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonObject;
import com.medica.ledproject.common.BxyUtil;
import com.medica.ledproject.common.CustomException;
import com.medica.ledproject.entity.*;
import com.medica.ledproject.mapper.DzParkLedProgramsMapper;
import com.medica.ledproject.mapper.DzParkLedscreeMapper;
import com.medica.ledproject.service.IDzParkLedProgramsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaona
 * @since 2024-05-29
 */
@Service
public class DzParkLedProgramsServiceImpl extends ServiceImpl<DzParkLedProgramsMapper, DzParkLedPrograms> implements IDzParkLedProgramsService {
   @Autowired
    DzParkLedProgramsMapper dzParkLedProgramsMapper;
    @Autowired
    DzParkLedscreeMapper dzParkLedscreeMapper;
    @Value("${uplodFilePath}")
    String uplodFilePath ;
    @Value("${filePath}")
    public  String filePath;
    @Override
    public String updatePrograms(DzParkLedPrograms dzParkLedPrograms) {
        DzParkLedPrograms p=dzParkLedProgramsMapper.selectById(dzParkLedPrograms.getId());
        if(p==null){
            throw new CustomException("要修改的节目不存在");
        }
        //dzParkLedPrograms.setCtime(LocalDateTime.now());
        return dzParkLedProgramsMapper.updateById(dzParkLedPrograms)>0?"修改成功":"修改失败";
    }

    @Override
    public String sendPrograms(RequestProN requestProN) {
        List<DzParkLedscree> dzList =new ArrayList<>();
        List<DzParkLedPrograms> prolist =new ArrayList<>();

        try {
            if((requestProN.getAdress()==null||requestProN.getAdress().length()==0)&&!requestProN.isAll()){
                throw new CustomException("发布地址不能为空");
            }
            if((requestProN.getProID()==null||requestProN.getProID().length()==0)&&!requestProN.isAll()){
                throw new CustomException("要发布的节目不能为空");
            }
            //查询地址列表
            if(requestProN.isAll()){
                dzList = dzParkLedscreeMapper.selectList(new QueryWrapper<DzParkLedscree>().eq("is_del","0"));
            }else{
                String[] array = requestProN.getAdress1();
                dzList = dzParkLedscreeMapper.selectList(new QueryWrapper<DzParkLedscree>().eq("is_del","0").in("id",requestProN.getAdress()));
            }
            //查询节目列表
            prolist =dzParkLedProgramsMapper.selectList(new QueryWrapper<DzParkLedPrograms>().ne("state","-1").in("id",requestProN.getProID()));

            if(dzList!=null&&dzList.size()>0){
                for(DzParkLedscree dz:dzList){
                    if(prolist!=null&&prolist.size()>0){
                        boolean scu=BxyUtil.sendPrograms(dz.getIp(),prolist,uplodFilePath);
                        if(scu){
                            for(DzParkLedPrograms pro:prolist){
                                    pro.setLedId(dz.getId());
                                    pro.setLampId(dz.getLampId());
                                    pro.setState(1);
                                    dzParkLedProgramsMapper.updateById(pro);

                            }
                        }
                    }else{
                        throw new CustomException("没有查询到要发布的节目");
                    }
                }
            }else{
                throw new CustomException("IP地址为空");
            }
        }catch (Exception e){
            throw new CustomException(e.getMessage());
        }

        return "发布成功";
    }

    @Override
    public String delPrograms(int id) {
        if(id==0){
            throw new CustomException("没有查询到要删除的节目");
        }
        DzParkLedPrograms d=dzParkLedProgramsMapper.selectById(id);
        if(d==null){
            throw new CustomException("没有查询到要删除的节目");
        }
        d.setState(-1);
        return dzParkLedProgramsMapper.updateById(d)>0?"删除成功":"删除失败";
    }

    @Override
    public IPage<DzParkLedProgramsRespon> getPrograms(ProgramRequestDao programRequestDao) {
        IPage<DzParkLedProgramsRespon> pages = new Page<>(programRequestDao.getPage(), programRequestDao.getPageSiza());

        pages=dzParkLedProgramsMapper.getProgramsPageList(pages,programRequestDao.getName(),programRequestDao.getLedName());
        List<DzParkLedProgramsRespon> list =pages.getRecords();
        if(list!=null&&list.size()>0){
            for (DzParkLedProgramsRespon dz:list){
                if(StringUtils.isNotBlank(dz.getPicUrl())) {
                    dz.setPicUrl(filePath + dz.getPicUrl());
                }
                if(StringUtils.isNotBlank(dz.getVideoUrl())) {
                    dz.setVideoUrl(filePath + dz.getVideoUrl());
                }
            }
        }
        pages.setRecords(list);
        return pages;
    }
}
